package com.gc.client;

import java.util.List;
import java.util.Vector;

import javax.activity.InvalidActivityException;
import javax.management.ReflectionException;

import com.gc.exception.CantEvaluateException;
import com.gc.exception.DuplicatedException;
import com.gc.exception.InvalidateException;
import com.gc.exception.RecordNotFoundException;
import com.gc.model.DramaEvaluateModel;
import com.gc.model.DramaModel;
import com.gc.model.GcCommunicationModel;
import com.gc.model.UserModel;
import com.gc.protocol.GcManager;

public class GcClientManager implements GcManager{
	private GcClientConnection gcClientConnection = GcClientConnection.getInstance();

	@Override
	public void signUp(UserModel userModel) throws DuplicatedException {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.SIGN_UP);
		Vector<Object> v = new Vector<Object>();
		v.add(userModel);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		int command = read.getCommand();
		
		if(GcManager.EXCEPTION_DUPLICATE == command)
			throw new DuplicatedException();
	}

	@Override
	public UserModel signIn(String id, String password) throws InvalidateException, RecordNotFoundException {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.SIGN_IN);
		Vector<Object> v = new Vector<Object>();
		v.add(id);
		v.add(password);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		
		int command = read.getCommand();
		if (command == GcManager.EXCEPTION_INVALIDATE){ 
				throw new InvalidateException();
		}else if (command ==GcManager.EXCEPTION_RECORD_NOT_FOUND){
				throw new RecordNotFoundException();
		}
		
		return (UserModel) read.getArgs().get(0);
	}

	@Override
	public void evaluateDrama(DramaEvaluateModel dem) throws CantEvaluateException{
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.EVALUATE_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(dem);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		int command = read.getCommand();
		if (command == GcManager.EXCEPTION_CANT_EVALUATE){ 
			throw new CantEvaluateException();
		}
	}

	@Override
	public List<DramaModel> getRecommendDrama(String userId) {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.SEARCH_RECOMMEND_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(userId);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		
		return (List<DramaModel>) read.getArgs().get(0);
	}

	@Override
	public void updateUser(UserModel userModel) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public List<DramaModel> searchDrama(String title) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deleteDrama(Integer dramaId) throws RecordNotFoundException {
		boolean result = false;
		
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.DELETE_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(dramaId);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		GcCommunicationModel read = gcClientConnection.read();
		
		int command = read.getCommand();
		if (command == GcManager.EXCEPTION_RECORD_NOT_FOUND) {
				throw new RecordNotFoundException();
		}
		return result;
	}

	@Override
	public void registDrama(DramaModel dramaModel) throws DuplicatedException {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.REGIST_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(dramaModel);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		GcCommunicationModel read = gcClientConnection.read();
		
		int command = read.getCommand();
		if (command == GcManager.EXCEPTION_DUPLICATE){ 
				throw new DuplicatedException();
		}
	}

	@Override
	public void updateDrama(DramaModel dramaModel)
			throws RecordNotFoundException {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.UPDATE_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(dramaModel);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		
		int command = read.getCommand();
		if (command == GcManager.EXCEPTION_RECORD_NOT_FOUND){ 
				throw new RecordNotFoundException();
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DramaModel> searchDramas() {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.SEARCH_ALL_DRAMA);
		Vector<Object> v = new Vector<Object>();
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		
		return (List<DramaModel>) read.getArgs().get(0);
	}

	@Override
	public List<DramaModel> searchDramas(String userId) {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.SEARCH_USER_ALL_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(userId);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		
		return (List<DramaModel>) read.getArgs().get(0);
	}

	@Override
	public DramaModel searchDetailDrama(Integer dramaId, String userId) {
		GcCommunicationModel gcModel = new GcCommunicationModel();
		gcModel.setCommand(GcManager.SEARCH_DETAIL_DRAMA);
		Vector<Object> v = new Vector<Object>();
		v.add(dramaId);
		v.add(userId);
		gcModel.setArgs(v);
		gcClientConnection.write(gcModel);
		
		GcCommunicationModel read = gcClientConnection.read();
		
		return (DramaModel) read.getArgs().get(0);
	}

}
